package com.cee.admin.modules.pm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cee.common.web.result.Result;
import com.cee.domain.modules.pm.entity.PmTechInfo;
import com.cee.domain.modules.pm.vo.PmTechInfoVo;
import com.cee.domain.modules.sys.entity.User;
import com.cee.ds.common.utils.ShiroUtils;
import com.cee.ds.modules.pm.service.IPmTechInfoService;
import com.cee.ds.modules.sys.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cee.admin.common.BaseController;
import java.util.Date;

/**
 * <p>
 * 技术资讯表 前端控制器
 * </p>
 *
 * @author autoGenerator
 * @since 2025-06-16
 */
@RestController
@RequestMapping("/api/pm/techInfo")
public class PmTechInfoController extends BaseController {

    @Autowired
    private IPmTechInfoService techInfoService;

    @Autowired
    private IUserService iUserService;


    @Operation(summary = "分页查询技术资讯")
    @GetMapping("/page")
    public Result<Page<PmTechInfoVo>> page(@RequestParam Long current, @RequestParam Long size, PmTechInfoVo query){
        return success(techInfoService.queryPageVo(new Page<>(current, size), query));
    }


    @Operation(summary = "新增技术资讯")
    @PostMapping("/save")
    @RequiresPermissions("pm:techInfo:save")
    public Result<Boolean> save(@RequestBody PmTechInfo info){
        info.setStatus("0");
        User currentUser = ShiroUtils.getCurrentUser();
        info.setCreateUserId(currentUser.getId());
        info.setCreateTime(new Date());
        return success(techInfoService.saveOrUpdate(info));
    }


    @Operation(summary = "修改技术资讯")
    @PostMapping("/update")
    @RequiresPermissions("pm:techInfo:update")
    public Result<Boolean> update(@RequestBody PmTechInfo info){
        return success(techInfoService.saveOrUpdate(info));
    }


    @Operation(summary = "发布技术资讯")
    @PostMapping("/publish/{id}")
    @RequiresPermissions("pm:techInfo:publish")
    public Result<Boolean> publish(@PathVariable("id") Long id){
        PmTechInfo info = new PmTechInfo();
        info.setId(id);
        info.setStatus("1");
        info.setPublishTime(new Date());
        return success(techInfoService.updateById(info));
    }


    @Operation(summary = "查看技术资讯")
    @GetMapping("/get/{id}")
    @RequiresPermissions("pm:techInfo:get")
    public Result<PmTechInfoVo> get(@PathVariable("id") Long id){
        PmTechInfo info = techInfoService.getById(id);
        if (info == null){
            return error("该技术资讯不存在");
        }
        User user = iUserService.getById(info.getCreateUserId());
        PmTechInfoVo infoVo = new PmTechInfoVo();
        BeanUtils.copyProperties(info, infoVo);
        infoVo.setCreateUserName(user.getName());
        return success(infoVo);
    }


    @Operation(summary = "删除技术资讯")
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("pm:techInfo:delete")
    public Result<Boolean> delete(@PathVariable("id") Long id){
        return success(techInfoService.removeInfoById(id));
    }


}
